define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function ($, undefined, Backend, Table, Form, Template) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'basedata/roommgr/index',
                    add_url: 'basedata/roommgr/add',
                    edit_url: 'basedata/roommgr/edit',
                    del_url: 'basedata/roommgr/del',
                    multi_url: 'basedata/roommgr/multi',
                    table: 'roommgr',
                }
            });

            Template.helper("Moment", Moment);
            Template.helper("addons", Config['device_detail_manager']);
            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                singleSelect: true, //是否启用单选
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'rmname', title: __('Rmname')},
                        {field: 'rmcode', title: __('Rmcode')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            //点击包含.btn-addtabs的元素时新增选项卡
            $(document).on('click', '.btn-addtabswithparams,.addtabsit', function (e) {
                var that = this;
                var options = $.extend({}, $(that).data() || {});
                if (typeof options.confirm !== 'undefined') {
                    Layer.confirm(options.confirm, function (index) {
                        Backend.api.addtabs(Backend.api.replaceids(that, $(that).attr('href')), $(that).attr("title"));
                        Layer.close(index);
                    });
                } else {
                    var ids = Table.api.selectedids(table);
                    var url = $(that).attr('href');
                    if (url.indexOf("{ids}") !== -1) {
                        url = Table.api.replaceurl(url, {ids: ids.length > 0 ? ids.join(",") : 0}, table);
                    }

                    Backend.api.addtabs(Backend.api.replaceids(that, url), $(that).attr("title"));
                }

                return false;
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});